package com.xmbandu.app.activity;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.xmbandu.app.R;
import com.xmbandu.app.adapter.BookAdapter;
import com.xmbandu.app.base.BaseActivity;
import com.xmbandu.app.bean.BookListBean;
import com.xmbandu.app.httpnet.BaseObserver;
import com.xmbandu.app.httpnet.RetrofitFactory;
import com.xmbandu.app.utils.CommonUtils;
import com.xmbandu.app.view.GridSpacingItemDecoration;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;

/**
 * Created by wmq on 2019/4/25.
 */

public class SearchActiviy extends BaseActivity {
    private RecyclerView rvBook;
    private BookAdapter bookAdapter;
    private List<BookListBean.ListBean> bookList;
    private TextView cancle;
    private EditText editText;
    private int page = 1;
    private String word = "";

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initViews() {
        rvBook = findViewById(R.id.recycle);
        cancle = findViewById(R.id.cancle);
        editText = findViewById(R.id.search_edit);
    }

    @Override
    public void initListener() {
        setOnClick(cancle);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 0 || actionId == 3) && event != null) {
                    if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                        word = editText.getText().toString().trim();
                        getList(editText.getText().toString().trim(), page);
                        CommonUtils.hideSoftInput(SearchActiviy.this);
                    }
                    return true;
                }
                return false;
            }
        });
    }

    @Override
    public void initData() {
        bookList = new ArrayList<>();
        bookAdapter = new BookAdapter(R.layout.item_book, bookList);
        rvBook.setLayoutManager(new GridLayoutManager(this, 2, LinearLayoutManager.VERTICAL, false));
        rvBook.addItemDecoration(new GridSpacingItemDecoration(2, CommonUtils.dip2px(this, 10), false));
        rvBook.setAdapter(bookAdapter);
        bookAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(SearchActiviy.this,"暂无详情页哦，我们还在努力！",Toast.LENGTH_SHORT).show();
            }
        });
        bookAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                if (bookAdapter.getData().size() < 20) {
                    bookAdapter.loadMoreEnd();
                } else {
                    loadMoreList(word, page);
                }
            }
        }, rvBook);
    }

    @Override
    public void processClick(View v) {
        switch (v.getId()) {
            case R.id.cancle:
                finish();
                break;
        }

    }

    private void getList(String keyword, int page) {
        bookList.clear();
        Observable observable = RetrofitFactory.getInstance().bookListResult(keyword, page);
        observable.compose(compose(this.bindToLifecycle())).subscribe(new BaseObserver<BookListBean>(this) {


            @Override
            protected void onHandleSuccess(BookListBean bookListBean) {
                if (bookListBean.getList().size() > 0) {
                    bookList.addAll(bookListBean.getList());
                    bookAdapter.notifyDataSetChanged();
                } else {
                    Toast.makeText(SearchActiviy.this, "暂无数据", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void loadMoreList(String keyword, int page) {
        Observable observable = RetrofitFactory.getInstance().bookListResult(keyword, page);
        observable.compose(compose(this.bindToLifecycle())).subscribe(new BaseObserver<BookListBean>(this) {


            @Override
            protected void onHandleSuccess(BookListBean bookListBean) {
                bookList.addAll(bookListBean.getList());
                if (bookListBean.getList().size() < 20) {
                    bookAdapter.loadMoreEnd();
                } else {
                    bookAdapter.loadMoreComplete();
                }
                bookAdapter.notifyDataSetChanged();
            }
        });
    }
}
